//
//  HWVipGiftBagSearchHeader.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/10.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit

class HWVipGiftBagSearchHeader: UIView {
    var bgImageView:UIImageView!
    var gameImageView:UIImageView!
    var gameNameLabel:UILabel!
    
    var searchLogo:UIImageView!
    var searchField:UITextField!
    var searchBgView:UIImageView!
    var searchGame:((_:String)->())?
//    class func headerOrFooterWithTableView(tableView:UITableView) -> (HWVipGiftBagSearchHeader) {
//        let reUse:String = "HWVipGiftBagSearchHeader"
//
//        var headerOrFooter = tableView.dequeueReusableHeaderFooterView(withIdentifier: reUse)
//
//        if headerOrFooter == nil {
//            headerOrFooter = HWVipGiftBagSearchHeader.init(reuseIdentifier: reUse)
//        }
//
//        return headerOrFooter as! HWVipGiftBagSearchHeader
//    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    override init(reuseIdentifier: String?) {
//        super.init(reuseIdentifier: reuseIdentifier)
//        initUI()
//        initFrame()
//    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        
        backgroundColor = UIColor.white
//        contentView.backgroundColor = .white
        
        bgImageView = UIImageView()
        bgImageView.image = UIImage(named: "giftBagSearchBg")
        addSubview(bgImageView)
        
        gameImageView = UIImageView()
        gameImageView.backgroundColor = .red
        gameImageView.layer.cornerRadius = 8
        gameImageView.layer.masksToBounds = true
        addSubview(gameImageView)
        
        gameNameLabel = HWLabel.createBoldLabel(title: "-", color: "#333333", font: 12)
        addSubview(gameNameLabel)
        
        searchBgView = UIImageView()
//        searchBgView.backgroundColor = .colorWithHexString(hexString: "#F3F4F8")
        searchBgView.image = UIImage(named: "giftBagSearch")
        searchBgView.isUserInteractionEnabled = true
        addSubview(searchBgView)
        
        searchLogo = UIImageView()
        searchLogo.image = UIImage(named: "giftBagSearchLogo")
        searchBgView.addSubview(searchLogo)
        
        searchField = UITextField()
        searchField.textColor = .white
        searchField.clearButtonMode = .whileEditing
        searchField.delegate = self
        searchField.textColor = .colorWithHexString(hexString: "#333333")
        searchField.font = .systemFont(ofSize: 12)
        searchBgView.addSubview(searchField)
        
        let placeHoldLabel = UILabel.init()
        placeHoldLabel.text = "搜索游戏"
        placeHoldLabel.textColor = UIColor.colorWithHexString(hexString: "#999999")
        placeHoldLabel.font = UIFont.systemFont(ofSize: 12)
        searchField.setValue(placeHoldLabel, forKey: "_placeholderLabel")
        searchField.addTarget(self, action: #selector(fieldFieldChange(_:)), for: .editingChanged)
        
        
    }
    
    func initFrame() -> Void {
        bgImageView.snp.makeConstraints { make in
            make.left.top.equalToSuperview()
            make.right.equalTo(-13*widthScale)
            make.height.equalTo(bgImageView.snp.width).multipliedBy(0.11)
        }
        
        gameImageView.snp.makeConstraints { make in
            make.left.equalTo(15*widthScale)
            make.top.equalTo(bgImageView.snp.bottom).offset(9*widthScale)
            make.width.height.equalTo(40*widthScale)
        }
        
        gameNameLabel.snp.makeConstraints { make in
            make.left.equalTo(gameImageView.snp.right).offset(4*widthScale)
            make.centerY.equalTo(gameImageView)
        }
        
        searchBgView.snp.makeConstraints { make in
            make.right.equalTo(-15*widthScale)
            make.top.equalTo(7*widthScale)
            make.bottom.equalTo(-7*widthScale)
            make.width.equalTo(screenWidth*0.61)
        }
        
        searchLogo.snp.makeConstraints { make in
            make.left.equalTo(10*widthScale)
            make.width.height.equalTo(12*widthScale)
            make.centerY.equalToSuperview()
        }
        
        searchField.snp.makeConstraints { make in
            make.left.equalTo(searchLogo.snp.right).offset(6*widthScale)
            make.centerY.equalToSuperview()
            make.right.equalTo(-10)
        }
        
    }
}
extension HWVipGiftBagSearchHeader:UITextFieldDelegate {
    @objc func fieldFieldChange(_ textField:UITextField) ->Void {
        if textField.text?.count ?? 0 > 0 {
//            placeHoldLabel.textColor = .white
//            lineViewOne.backgroundColor = .white
        }else {
//            placeHoldLabel.textColor = .colorWithHexString(hexString: "#999999")
//            lineViewOne.backgroundColor = .colorWithHexString(hexString: "#999999")
            self.searchGame?("")
        }
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.searchGame?(textField.text ?? "")
        return true
    }
}
